<?php
/**
 * Created by PhpStorm.
 * User: liuyu
 * Date: 2018/11/19
 * Time: 11:19
 */

namespace app\api\controller\v15;

use app\api\controller\v2\Auth;
use app\common\model\ArticleComment;
use app\common\model\ArticleCommentReply;
use app\common\model\MessageCenter;
use app\common\model\MiddleInformant;
use think\Cache;
use XznsTools\cache\CacheManage;

/**
 * 内容申诉
 * @version 1.10.2
 * Class ComplaintComment
 * @package app\api\controller\v15
 */
class Complain extends Auth
{
    /**
     * 申诉
     * @throws \Exception
     */
    public function appeal()
    {
        $complain_id = input('param.complain_id/d', 0);//举报的内容id
        $mid = input('param.mid/d', 0);//消息id
        if (!$complain_id || !$mid)
        {
            return show(config('code.error'), '请求信息未知，请稍后再试');
        }
        $MsgModel = new MessageCenter();
        $msg = $MsgModel->batchGetInfoByIds($mid);
        $msg = $msg ? current($msg) : [];
        $commentModel = new ArticleComment();
        $replyModel = new ArticleCommentReply();
        if ($msg && $msg['range'] == $this->user['id'])
        {
            //评论申诉
            if ($msg['article_comment_id'])
            {
                $type = 1;
                $id = $msg['article_comment_id'];
                //申诉状态非未申诉
                if ($msg['ac_check_status'] != 1)
                {
                    return show(config('code.error'), '您已申诉');
                }
                //修改评论表审核状态
                $commentModel->where('id', $id)->update(['check_status' => 2]);
                Cache::rm('article_comment_' . $id);  // 删除对应评论内容缓存
                Cache::rm('article_comment_good_' . $id); // 删除对应评论点赞缓存
            }//回复申诉
            elseif ($msg['article_comment_reply_id'])
            {
                $type = 3;
                $id = $msg['article_comment_reply_id'];
                //申诉状态非未申诉
                if ($msg['cr_check_status'] != 1)
                {
                    return show(config('code.error'), '您已申诉');
                }
                //回复表审核状态
                $replyModel->where('id', $id)->update(['check_status' => 2]);
                Cache::rm('comment_reply_id_' . $id); //删除对应回复id数组
                Cache::rm('user:reply:' . $this->user['id']); // 删除对应用户回复id数组
            } else
            {
                return show(config('code.error'), '未知的申诉内容');
            }
            //将消息改为已读状态
            $MsgModel->UpdateData($this->user['id'], $msg['type'], [$mid], ['is_read' => 1]);
            //更新消息详情缓存
            CacheManage::getInstance()->setCacheConfig((new MessageCenter())->cacheModel, $mid)->rmCache();
            //申诉添加举报表
            $data = [
                'type' => $type,
                'comment_id' => $id,
                'info_id' => $complain_id,
                'uid' => $this->user['id'],
                'create_time' => time()
            ];
            (new MiddleInformant())->SaveData($data);
            return show(config('code.success'), '申诉成功');
        }
        return show(config('code.error'), '申诉失败');
    }
}
